﻿#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "CEFrame.h"
#include "CECascadeSystem.h"

#include <wx/colordlg.h> 

///------------------------------------------------------------
// CEFrame
///------------------------------------------------------------
BEGIN_EVENT_TABLE( CEFrame, wxFrame )
	EVT_CLOSE( CEFrame::OnClose )
	EVT_MENU( idMenuQuit, CEFrame::OnQuit )
	EVT_MENU( idImport, CEFrame::OnImport )
	EVT_MENU( idExport, CEFrame::OnExport )
	EVT_MENU( idMenuAbout, CEFrame::OnAbout )
	EVT_BUTTON( idBGColor, CEFrame::OnBGColor )
	EVT_TOGGLEBUTTON( idDrawCoordinate, CEFrame::OnDrawCoord ) 
	EVT_TOGGLEBUTTON( idPause, CEFrame::OnPause )
	EVT_BUTTON( idReset, CEFrame::OnReset )
END_EVENT_TABLE()

CEFrame::CEFrame( wxFrame *frame, const wxString& title )
	: wxFrame( frame, -1, title, wxDefaultPosition, wxSize( 1100,700 ) )
{

	SetMinSize( wxSize( 600, 400 ) );

	//#if wxUSE_MENUS
	// create a menu bar
	wxMenuBar* mbar = new wxMenuBar();
	wxMenu* fileMenu = new wxMenu( L"" );
	fileMenu->Append( idImport, L"Import" );
	fileMenu->AppendSeparator();
	fileMenu->Append( idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
	mbar->Append( fileMenu, _("&File"));

	wxMenu* helpMenu = new wxMenu(_T(""));
	helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
	mbar->Append(helpMenu, _("&Help"));
	SetMenuBar(mbar);
	//#endif // wxUSE_MENUS

	//#if wxUSE_STATUSBAR
	// create a status bar with some information about the used wxWidgets version
	CreateStatusBar(2);
	SetStatusText(_(""),0);
	SetStatusText(_(""), 1);
	//#endif // wxUSE_STATUSBAR
	wxToolBar* pToolBar = new wxToolBar( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL | wxNO_BORDER );

	//pToolBar->AddTool(wxID_OPEN, bmpSave, L"Open"));
	wxColor btnColour( 128, 128, 192 );

	// PauseBtn
	m_pPauseBtn = new wxToggleButton(pToolBar, idPause, L"Pause", wxPoint( 0, 0 ), wxSize( 50, 25 ) );
	m_pPauseBtn->SetValue( false );
	//m_pPauseBtn->SetBackgroundColour( btnColour );
	pToolBar->AddControl( m_pPauseBtn );

	// BGColourBtn

	m_pBGColorBtn = new wxButton( pToolBar, idBGColor, L"BGC", wxPoint( 50, 0 ), wxSize( 50, 25 ) );
	m_pBGColorBtn->SetBackgroundColour( btnColour ); 
	pToolBar->AddControl( m_pBGColorBtn );
	SetToolBar( pToolBar );

	// DrawCoordBtn
	m_pDrawCoordBtn = new wxToggleButton( pToolBar, idDrawCoordinate, L"Coord", wxPoint( 100, 0 ), wxSize( 50, 25) );
	m_pDrawCoordBtn->SetValue( true );
	//m_pDrawCoordBtn->SetBackgroundColour( btnColour ); 
	pToolBar->AddControl( m_pDrawCoordBtn );

	// ResetBtn
	m_pResetBtn = new wxButton( pToolBar, idReset, L"Reset", wxPoint( 150, 0 ), wxSize( 50, 25) );
	m_pResetBtn->SetBackgroundColour( btnColour ); 
	pToolBar->AddControl( m_pResetBtn );

	m_pTargetViewer = new CETargetViewer( this );
}

CEFrame::~CEFrame()
{

}

void CEFrame::OnClose( wxCloseEvent &event )
{
	Destroy();
}

void CEFrame::OnQuit( wxCommandEvent &event )
{
	Destroy();
}

void CEFrame::OnAbout( wxCommandEvent &event )
{
	//SetViewerClearColor();
}

void CEFrame::OnBGColor( wxCommandEvent &event )
{
	wxColourDialog cd( this );
	if ( wxID_OK == cd.ShowModal() )
	{
		wxColor color = cd.GetColourData().GetColour();
		XMFLOAT4 vColor( (float)color.Red() / 255, 
			(float)color.Green() / 255,
			(float)color.Blue() / 255, 
			1.0f );
		CEGetCascadeSystem()->SetBGColor( vColor );
	}
}

void CEFrame::OnDrawCoord( wxCommandEvent & event )
{
	CEGetCascadeSystem()->SetDrawCoordinate( m_pDrawCoordBtn->GetValue() );
}

void CEFrame::OnPause( wxCommandEvent &event )
{
	CEGetCascadeSystem()->SetPause( m_pPauseBtn->GetValue() );
}

void CEFrame::OnReset( wxCommandEvent &event )
{
	CEGetCascadeSystem()->InitEmitter();
}

void CEFrame::OnImport( wxCommandEvent &event )
{
	wxFileDialog* pFD = new wxFileDialog( this );
	pFD->SetWildcard( L"SPF files (*.spf)|*.spf" );	
	pFD->ShowModal();

	if ( pFD->GetPath() != L"" )
	{
		if ( pFD->GetPath().EndsWith( ".spf" ) )
		{	
			CEGetCascadeSystem()->Import( pFD->GetPath() );
		}
		else
		{
			wxMessageBox( L"The file is not available", L"Error" ); 
		}
	}
}

void CEFrame::OnExport( wxCommandEvent & event )
{
	wxMessageBox(L"export",L"");
}
